define(['jquery','echarts','pages/index/model','api','util'],function($,echarts,cfg,api,util){
 	var curType=1,
 	    timer,
 	    isShow=false,
 	    isInit=false,
 	    option,
     	chart1Ct,
		chart2Ct,
     	chart1,
     	chart2,
     	charts1Rendered=false,
     	charts2Rendered=false;
     	
    var d= {
        curType:function(){return curType},
        doSwitch:function(type){
       	 
        	curType=type;
        	chart1Ct[type==1?'show':'hide']();
        	chart2Ct[type!=1?'show':'hide']();

            $(".time td a").removeClass('selected');
            this.loadData(type);
            $(".time td a[data=" + type + "]").addClass("selected");
        },
        loadData:function(type){
        	var charts=this,
        	    mindecimal=cfg.goods_obj[cfg.groupid][0].mindecimal,
        	    fixedNumber=Math.pow(10,mindecimal);
             
            api.charts({
                data: {datatype: type==1?0:type, goodscode:cfg.groupid,count:type==1?180:30},
                callback: function (rs) {
                   var temp={
                       data:[],
                       mindecimal:mindecimal,
                       time:[]
                   },row;
                   if(rs.retcode=='0'){
                   	  for(var i=0;i<rs.data.length;i++){
                   	      row=rs.data[i];
	                   	  if(type==1){
	                   	  	 temp.data.push(row[0]/fixedNumber);
	                   	  	 temp.time.push(new Date(+row[1]*1000).format('h:mm'));
	                   	  }else{
	                   	     temp.data.push([row[0]/fixedNumber,row[3]/fixedNumber,row[2]/fixedNumber,row[1]/fixedNumber])//open,close,low,high
	                   	     temp.time.push(new Date(+row[4]*1000).format('h:mm'));
	                   	  }
                   	  }
               	   	  charts.renderData(type,temp);
                   }
                }
            });
        },
        renderData:function(type,data){
            var precision=data.mindecimal ? data.mindecimal : 1 ,
                format=function (v) {
	                return  data.mindecimal?v.toFixed(data.mindecimal):parseInt(v);
	             },
	             x;
            if(type==1){
            	x=12+(data.data[0]+'').length*7;
           	 	option={
           	        color : ['#1BBCDC'],
           	        backgroundColor: null,
           	        tooltip: {
           	            trigger: 'axis',
           	            showDelay: 0             // 显示延迟，添加显示延迟可以避免频繁切换，单位ms
           	        },
           	        legend: {
           	            y: -30,
           	            data: ['金诚指数', '最新价格', '虚拟数据']
           	        },
           	        grid: {
           	            x: x,
           	            y: 10,
           	            x2: 10,
           	            y2: 30,
           	            backgroundColor: 'rgba(0,0,0,0)',
           	            borderWidth: 0,
           	            borderColor: 'rgba(0,0,0,0)'
           	        },
           	        dataZoom: {
           	            y: 250,
           	            show: false,
           	            realtime: true,
           	            start: 0,
           	            end: 100

           	        },
           	        xAxis: [
           	            {
           	                axisLine: {// 轴线
           	                    show: true,
           	                    lineStyle: {
           	                        color: '#FFF',
           	                        width: 2
           	                    }
           	                },
           	                type: 'category',
           	                boundaryGap: true,
           	                axisTick: {show: false},
           	                splitLine: {show: false},
           	                axisLabel: {show: true},
           	                data: data.time
           	            }
           	        ],
           	        yAxis: [
           	            {
           	                type: 'value',
           	                axisLine: {
           	                    show: true,
           	                    lineStyle: {
           	                        color: '#FFF',
           	                        width: 2
           	                    }
           	                },
           	                scale: '_max',
           	                scaleSize: 5,
           	                precision: precision,
           	                boundaryGap: true,
           	                splitArea: {
           	                    show: false
           	                }
           	                ,
           	                axisLabel: {
           	                    formatter: format
           	                }


           	            }
           	        ],
           	        series: [
           	            {
           	                name: '最新价格',
           	                type: 'line',
           	                symbol: 'none',
           	            smooth:true,
           	            itemStyle: {normal: {areaStyle: {type: 'default'}}},
           	                data:data.data,
           	                precision: 2 
           	            }
           	        ]
           	    };
           if(!charts1Rendered){
             	chart1.setOption(option);
             	charts1Rendered=true;
            }else{
            	chart1.dispose();
            	chart1=echarts.init(chart1Ct.get(0));
            	chart1.setOption(option)
            }
        }else{
       	 	option ={
   		        grid: {
   		            x: 50,
   		            y: 10,
   		            x2: 0,
   		            y2: 30,
   		            backgroundColor: 'rgba(0,0,0,0)',
   		            borderWidth: 0,
   		            borderColor: 'rgba(0,0,0,0)'
   		        },
   		        tooltip: {
   		            trigger: 'axis',
   		            axisPointer: {
   		                type: 'line'
   		            },
   		            formatter: function(params, ticket, callback) {
   		                var f = params[0].name;
   		                f += '<br/>  开盘 : ' + params[0].data[0] + '  最高 : ' + params[0].data[3];
   		                f += '<br/>  收盘 : ' + params[0].data[1] + '  最低 : ' + params[0].data[2];
   		                return f
   		            }
   		        },
   		        xAxis: {
   		            type: 'category',
   		            data: data.time,
   		            scale: true,
   		            boundaryGap: true,//['10%','10%'],
   		            splitNumber: 20,
   		            axisLine: {// 轴线
   		                show: false,
   		                onZero:true,
   		                lineStyle: {
   		                    color: '#fff',
   		                    width: 2
   		                }
   		            }
   		        },
   		        yAxis: {
   		            type: 'value',
   		            axisLine: {
   		                show: false,
   		                lineStyle: {
   		                    color: '#fff',
   		                    width: 2
   		                }
   		            },
   		            scale: '_max',
   		            precision: precision,
   		            boundaryGap: [0.001, 0.001],
   		            splitArea: {
   		                show: false
   		            },
   		            axisLabel: {
   		                formatter: format
   		            },
   		            splitArea: {
   		                show: false
   		            },
   		            scaleSize: 5
   		        },
   		        series: [
   		            {
   		                type: 'candlestick',
   		                data: data.data,
   		                itemStyle: {
   		                    normal: {
   		                        color: '#d87a80', // 阳线填充颜色
   		                        color0: '#2ec7c9', // 阴线填充颜色
   		                        borderColor: '#d87a80',
   		                        borderColor0: '#2ec7c9'

       		                    }
       		                }
       		            }
       		        ]
       		 };
           	 if(!charts2Rendered){
             	chart2.setOption(option);
             	charts2Rendered=true;
            }else{
            	chart2.dispose();
            	chart2=echarts.init(chart2Ct.get(0));
            	chart2.setOption(option);
            }
            }
    	},
    	event:function(){
    		var ct=$('#charts_nav'),
    		    that=this;
    		ct.on('click','a',function(){
    		    var type=$(this).attr('data');
    		    that.doSwitch(type);
    		})
    	},
    	init:function(){
    		if(!isInit){
	    		chart1Ct=$('#charts1'),
			    chart2Ct=$('#charts2'),
	     	    chart1=echarts.init(chart1Ct.get(0)),
	     	    chart2=echarts.init(chart2Ct.get(0)),
	     	    this.doSwitch(1);
	     	    this.event();
	     	    isInit=true;
    		}
    		isShow=true;
			timer=setTimeout(function(){
				if(d.curType()==1){
					d.loadData(1);
				}
				if(isShow)timer=setTimeout(arguments.callee,cfg.refreshChart);
			},cfg.refreshChart);
			
    	},
    	clearTimer:function(){
    		clearTimeout(timer);
    		isShow=false;
    	}
    }
	return d;
})